package com.oa.server.controller;


import com.oa.server.entity.*;
import com.oa.server.result.ResponseBean;
import com.oa.server.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mcy
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;
    @Autowired
    IPoliticsStatusService politicsStatusService;
    @Autowired
    IJoblevelService joblevelService;
    @Autowired
    INationService nationService;
    @Autowired
    IPositionService positionService;
    @Autowired
    IDepartmentService departmentService;

    @ApiOperation(value = "获取所有员工(分页)")
    @GetMapping("/")
    public RespPageBean getAllEmployees(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        Employee employee, LocalDate[] beginDateScope){
            return employeeService.getAllEmployeesByPage(currentPage,pageSize,employee,beginDateScope);
    }

    @ApiOperation(value = "获取所有政治面貌")
    @GetMapping("/politicsStatus")
    public List<PoliticsStatus> getAllPoliticsStatus(){
        return politicsStatusService.list();
    }
    @ApiOperation(value = "获取所有职称")
    @GetMapping("/joblevel")
    public List<Joblevel> getAllJoblevels(){
        return joblevelService.list();
    }
    @ApiOperation(value = "获取所有民族")
    @GetMapping("/nations")
    public List<Nation> getAllNations(){
        return nationService.list();
    }
    @ApiOperation(value = "获取所有职位")
    @GetMapping("/positions")
    public List<Position> getAllPositions(){
        return positionService.list();
    }
    @ApiOperation(value = "获取所有部门")
    @GetMapping("/getAllDepartments")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }
    @ApiOperation(value = "获取最后的员工的工号=当前最大工号")
    @PostMapping("/maxWorkID")
    public ResponseBean getMaxWorkID(){
        return employeeService.getMaxWorkID();
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/addEmpployee")
    public ResponseBean addEmployee(@RequestBody Employee employee){
        return employeeService.addEmployee(employee);
    }
    @ApiOperation(value = "更新员工")
    @PutMapping("/updateEmployee")
    public  ResponseBean updateEmployee(@RequestBody Employee employee){
        if(employeeService.updateById(employee)){
            return ResponseBean.success("更新成功！");
        }
        return ResponseBean.error("更新失败");
    }

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    public ResponseBean deleteEmployee(@PathVariable Integer id){
        if(employeeService.removeById(id)){
            return ResponseBean.success("删除成功！");
        }
        return ResponseBean.error("删除失败!");
    }

    /**
     * 使用流形式导出
     */
    @ApiOperation(value ="导出员工数据" )
    @GetMapping(value = "/export")
    public void exportEmployee(HttpServletResponse servletResponse){
        //获取员工的数据
        employeeService.getEmployee(null);
    }
}
